package com.itelite.service.impl;

import com.itelite.common.info.RedisKey;
import com.itelite.common.util.FileUtils;
import com.itelite.config.WebAppConfig;
import com.itelite.domain.dto.Carousel;
import com.itelite.domain.pojo.Config;
import com.itelite.mapper.ConfigMapper;
import com.itelite.service.ConfigService;
import com.itelite.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 * @create 2021/8/9-15:05
 **/
@Service
@Slf4j

public class ConfigServiceImpl implements ConfigService {
    @Autowired
    private RedisService redisService;
    @Autowired
    private ConfigMapper configMapper;

    @Override
    @SuppressWarnings("unchecked")
    public List<Carousel> getCarousel() {
        List<Object> list = redisService.getList(RedisKey.CAROUSEL,0,1);
        if(!CollectionUtils.isEmpty(list)){
            return (List<Carousel>)list.get(0);
        }
        List<Carousel> carousels = configMapper.queryCarousels();
        if(!CollectionUtils.isEmpty(carousels)){
            redisService.cacheList(RedisKey.CAROUSEL,carousels);
        }
        return carousels;
    }

    @Override
    public Config getBackGround() {
        return configMapper.queryBackGround();
    }

    @Override
    public boolean deleteCarousel(Integer carouselId) {
        Config carousel = configMapper.queryCarousel(carouselId);
        String url = carousel.getConfig();
        File file = new File(WebAppConfig.ROOT+url);
        if(file.exists()&&file.delete()){
            configMapper.deleteConfig(carouselId);
            return true;
        }
        return false;
    }

    @Override
    public boolean uploadCarousel(MultipartFile file) {
        //上传图片
        File coverDir = FileUtils.createDir(WebAppConfig.ROOT_DIR_CONFIG);
        //插入文件信息到数据库中
        String fileName = "1-"+UUID.randomUUID() +".jpg";
        File desFile = new File(coverDir, fileName);
        try {
            file.transferTo(desFile);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("上传轮播图片失败");
        }
        Config config = new Config();
        config.setConfig(File.separator+ WebAppConfig.ROOT_DIR_CONFIG+File.separator+desFile.getName());
        config.setType(1);
        config.setState(1);
        return configMapper.insertConfig(config)>0;
    }

    @Override
    public boolean uploadBackGroup(MultipartFile file) {
        Config c = configMapper.queryBackGround();
        if(c!=null){
            String url = c.getConfig();
            File f = new File(WebAppConfig.ROOT+url);
            //删除图片
            if(f.exists()) f.delete();
            configMapper.deleteConfig(c.getConfigId());
        }
        //上传图片
        File coverDir = FileUtils.createDir(WebAppConfig.ROOT_DIR_CONFIG);
        //插入文件信息到数据库中
        String fileName = "2-"+UUID.randomUUID() +".jpg";
        File desFile = new File(coverDir, fileName);
        Config config = new Config();
        try {
            file.transferTo(desFile);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("上传背景图片失败");
        }
        config.setConfig(File.separator+ WebAppConfig.ROOT_DIR_CONFIG+File.separator+desFile.getName());
        config.setType(2);
        config.setState(1);
        return configMapper.insertConfig(config)>0;
    }

    @Override
    public boolean updateSendState(Integer state) {
        configMapper.updateSendEmailState(state);
        return true;
    }

    @Override
    public int getSendState() {
        Config c = configMapper.queryConfig(4);
        return c==null?0:1;
    }
}
